import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userStore } from '@/store/user.store'
import routers from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const routes: Array<RouteRecordRaw> = routers

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (to.name === 'Signin' && userStore().profile) {
    return next('/home')
  }
  if (to.meta.tip) {
    ElMessage.info(to.meta.tip as string)
    return next(from.path || '/')
  }
  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
